#include "GUIForm.h"

GUIForm::GUIForm()
{
    current = 0;
    pressedId = 0;
}

GUIForm::~GUIForm()
{

}

// Associate an element with the form
void GUIForm::add(GUIElement* elem)
{
    elements.push_back(elem);
}

// Select next element by triggering a hover effect
void GUIForm::next()
{
    // Return early if empty
    if (elements.size() == 0)
        return ;

    // Make sure current button is set back to normal state
    elements[current]->setState(Normal);

    // Revert to beginning if gone too far
    if (current + 1 >= elements.size())
    {
        current = 0;
    }
    else
    {
        current++;
    }

    // Force element to hover.
    elements[current]->setState(Hover);
}

// Select next element by triggering a hover effect
void GUIForm::previous()
{
    // Return early if empty
    if (elements.size() == 0)
        return ;

    // Make sure current button is set back to normal state.
    elements[current]->setState(Normal);

    // Revert to end if gone too far
    if (current - 1 < 0)
    {
        current = elements.size() - 1; // Set as last element in vector
    }
    else
    {
        current--;
    }

    // Force element to hover.
    elements[current]->setState(Hover);
}

void GUIForm::clear()
{
    elements.clear();
}

void GUIForm::onPressed()
{
    elements[current]->setState(Active);
    elements[current]->onPressed();
    pressedId = current;
}

void GUIForm::onReleased()
{
    // Return early if the element the user started out pressing is different than
    // the element receiving the onReleased() state.
    if (pressedId != current)
        return ;

    elements[current]->onReleased();
    // Return element state to hover
    elements[current]->setState(Hover);
}
